package com.cskaoyan.controller.system;

import com.cskaoyan.bean.MallAdmin;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.PageListVo;
import com.cskaoyan.model.vo.system.SimpleMallAdmin;
import com.cskaoyan.service.system.AdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @program: project-market
 * @description: 管理员管理
 * @author: Weiwei Liu
 * @create: 2021-11-29 20:48
 **/
@RestController
@RequestMapping("admin/admin")
public class AdminController {



    @Autowired
    AdminService adminService;

    /**
     * 显示所有管理员列表，分页查询
     */
    @RequiresPermissions("admin:admin:list")
    @RequestMapping("list")
    public BaseRespVo list(PageBo pageBo, String username) {

        PageListVo vo = adminService.list(pageBo, username);

        return BaseRespVo.ok(vo);

    }

    /**
     * 新增管理员
     *
     * @param
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody SimpleMallAdmin simpleMallAdmin) {

        MallAdmin vo = adminService.create(simpleMallAdmin);

        if (vo == null) return BaseRespVo.manual(640, "管理员已经存在", null);

        return BaseRespVo.ok(vo);

    }

    /**
     * 删除管理员，修改管理员delete状态为1
     */
    @RequiresPermissions("admin:admin:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MallAdmin mallAdmin) {


        boolean status = adminService.delete(mallAdmin);

        if (status) return BaseRespVo.ok();

        return BaseRespVo.fail("删除失败");

    }


    /**
     * 更新管理员
     */
    @RequiresPermissions("admin:admin:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MallAdmin mallAdmin) {


        boolean status = adminService.update(mallAdmin);

        if (status) return BaseRespVo.ok();

        return BaseRespVo.fail("更新失败");

    }

}
